package com.mikao.controller;

import com.alibaba.fastjson.JSON;
import com.mikao.dto.bo.TestBO;
import com.mikao.dto.vo.BaseResult;
import com.mikao.service.AuthService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;


/**
 * @author KennyDavid
 */
@Slf4j
@Api("测试")
@RestController
@RequestMapping("/test")
@Scope("prototype")
@Validated
@ApiIgnore
public class TestController extends BaseController {

    @Autowired
    private AuthService authService;

    @ApiOperation(value = "非封装的参数校验")
    @GetMapping("/testValidate")
    public BaseResult testValidate(@RequestParam(required = false) @NotBlank(message = "p1不能为空") String p1,
                                   @RequestParam(required = false) @Length(min = 3, max = 5, message = "p2长度只能在{min}到{max}之间") String p2) {
        return BaseResult.getDefaultSuccessResult();
    }

    @ApiOperation(value = "测试POST方法")
    @PostMapping("/auth/post")
    public BaseResult test(@ApiParam(value = "测试表单") @RequestBody @Validated({TestBO.Post.class}) TestBO testBo, BindingResult bindingResult) {
        log.info("post testBo {}", JSON.toJSONString(testBo));
        return BaseResult.getDefaultSuccessResult();
    }

    @ApiOperation(value = "测试PUT方法")
    @PutMapping("/auth/put")
    public BaseResult test1(@ApiParam(value = "测试表单") @RequestBody @Validated({TestBO.Put.class}) TestBO testBo, BindingResult bindingResult) {
        log.info("put testBo {}", JSON.toJSONString(testBo));
        return BaseResult.getDefaultSuccessResult();
    }

    @ApiOperation(value = "获取token")
    @GetMapping("/getToken")
    public BaseResult token() {
        String token = authService.createToken(1111L);
        return new BaseResult(token);
    }
}
